﻿Imports System.ComponentModel
Imports System.Runtime.Serialization

''' <summary>
''' Base class for our LINQ to SQL classes.
''' This class demonstrates one way to implement the IDataErrorInfo
'''  interface so that the ErrorProvider and DataGridView can display
'''  validation errors in the UI.
''' </summary>
''' <remarks></remarks>
<DataContract()> _
Public Class BaseBusiness
    Implements IDataErrorInfo

    Private m_isDirty As Boolean
    ''' <summary>
    ''' Returns true if any of the properties have changed
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <DataMember()> _
    Public Property IsDirty() As Boolean
        Get
            Return m_isDirty
        End Get
        Set(ByVal value As Boolean)
            m_isDirty = value
        End Set
    End Property

    'This dictionary contains a list of our validation errors for each field
    Private m_validationErrors As New Dictionary(Of String, String)

    ''' <summary>
    ''' Returns a list of validation errors
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <DataMember()> _
    Public Property ValidationErrors() As Dictionary(Of String, String)
        Get
            Return m_validationErrors
        End Get
        Set(ByVal value As Dictionary(Of String, String))
            m_validationErrors = value
        End Set
    End Property

    Protected Sub AddError(ByVal columnName As String, ByVal msg As String)
        If Not m_validationErrors.ContainsKey(columnName) Then
            m_validationErrors.Add(columnName, msg)
        End If
    End Sub

    Protected Sub RemoveError(ByVal columnName As String)
        If m_validationErrors.ContainsKey(columnName) Then
            m_validationErrors.Remove(columnName)
        End If
    End Sub

    ''' <summary>
    ''' Returns True if any properties are invalid.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overridable ReadOnly Property HasErrors() As Boolean
        Get
            Return (m_validationErrors.Count > 0)
        End Get
    End Property

    ''' <summary>
    ''' Returns a string indicating any errors on the object 
    ''' (Nothing/null if no errors)
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property [Error]() As String _
        Implements System.ComponentModel.IDataErrorInfo.Error
        Get
            If m_validationErrors.Count > 0 Then
                Dim s As New System.Text.StringBuilder
                For Each e In m_validationErrors
                    s.Append(e.ToString + vbCrLf)
                Next
                Return s.ToString 'String.Format("{0} data is invalid.", TypeName(Me))
            Else
                Return Nothing
            End If
        End Get
    End Property

    ''' <summary>
    ''' Returns any validation errors associated with the column name
    ''' </summary>
    ''' <param name="columnName"></param>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Default Public ReadOnly Property Item(ByVal columnName As String) As String _
        Implements System.ComponentModel.IDataErrorInfo.Item
        Get
            If m_validationErrors.ContainsKey(columnName) Then
                Return m_validationErrors(columnName).ToString
            Else
                Return Nothing
            End If
        End Get
    End Property

    ''' <summary>
    ''' When overridden in a derived class, validates all properties.
    ''' Throws an exception if validation failed.
    ''' </summary>
    ''' <remarks></remarks>
    Public Overridable Sub Validate()
    End Sub

End Class
